package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.MonthBean;
import com.weimei.conversionTreasure.http.entity.MonthDetailBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.weight.BottomTimePop;
import com.weimei.conversionTreasure.weight.BottomWheelPop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MonthDetailVM extends TitleVM{
    public MonthDetailVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("账单详情");
    }

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }

    public ObservableField<String> time = new ObservableField<>("0");
    public ObservableField<String> types = new ObservableField<>("支出");
    public ObservableField<String> type = new ObservableField<>("0");
    public ObservableField<String> zZc = new ObservableField<>("");
    public ObservableField<String> zBs = new ObservableField<>("");


    public BindingCommand typePick=  new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            List<String> list;
            if (type.get().equals("支出")){
                list = Arrays.asList(mActivity.getResources().getStringArray(R.array.zc_str));
            }else {
                list = Arrays.asList(mActivity.getResources().getStringArray(R.array.sy_str));
            }
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomWheelPop(mActivity,list, s1 -> {
                        types.set(s1);
                        onResume();
                    }))
                    .show();
        }
    });

    public BindingCommand leftClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            int curDate;
            if (time.get().length() == 6){
                curDate = Integer.parseInt(time.get().substring(5,6));
            }else {
                curDate = Integer.parseInt(time.get().substring(5,7));
            }
            int js = curDate -1;
            if (js > 0){
                time.set(time.get().substring(0,4)+"-"+js+"");
            }
            onResume();

        }
    });

    public BindingCommand rightClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
                int curDate;
                if (time.get().length() == 6){
                    curDate = Integer.parseInt(time.get().substring(5,6));
                }else {
                    curDate = Integer.parseInt(time.get().substring(5,7));
                }
                int js = curDate+1;
                if (js <= 12){
                    time.set(time.get().substring(0,4)+"-"+js+"");
                }

            onResume();
        }
    });

    public BindingCommand timePick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomTimePop(mActivity, s1 -> {
                        time.set(s1);
                        onResume();
                    },true,false))
                    .show();
        }
    });

    @Override
    public void onResume() {
        super.onResume();
        getMonthData();
    }

    public ObservableList<MonthDetailItemVM> observableListM = new ObservableArrayList<>();
    public ItemBinding<MonthDetailItemVM> itemBindingM = ItemBinding.of(BR.viewModel, R.layout.item_month_detail);

    public void getMonthData(){
        observableListM.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("date", time.get());
        map.put("types", type.get());
        map.put("category", types.get());

        model.month_details(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<MonthDetailBean>() {
                    @Override
                    public void onNext(MonthDetailBean bean) {
                        if (bean.issucc){
                            zZc.set(bean.getData().getTotalExpend()+"");
                            zBs.set(bean.getData().getTotalNumber()+"");
                            if (bean.getData().getDInfo().size() > 0){
                                for (MonthDetailBean.DataDTO.DInfoDTO data: bean.getData().getDInfo()) {
                                    MonthDetailItemVM itemVM = new MonthDetailItemVM(MonthDetailVM.this,data);
                                    observableListM.add(itemVM);
                                }
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
